package com.itheima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import com.itheima.mall.mapper.HomeAdvertiseServiceMapper;
import com.itheima.mall.pojo.HomeAdvertise;
import com.itheima.mall.service.SmsHomeAdvertiseService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SmsHomeAdvertiseServiceImpl extends ServiceImpl<HomeAdvertiseServiceMapper, HomeAdvertise> implements SmsHomeAdvertiseService {

    @Autowired
    private HomeAdvertiseServiceMapper homeAdvertiseServiceMapper;

    /**
     * 分页查询广告
     */
    @Override
    public PageResult getList(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {
        try {
            Page<HomeAdvertise> page = new Page<>(pageNum,pageSize);
            Date endDate = null;
            if (endTime != null){
                endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
            }
            LambdaQueryWrapper<HomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(endTime != null, HomeAdvertise::getEndTime,endDate)
                    .like(name != null,HomeAdvertise::getName,name)
                    .eq(type != null,HomeAdvertise::getType,type);
            Page<HomeAdvertise> pageInfo = homeAdvertiseServiceMapper.selectPage(page, queryWrapper);
            return new PageResult(pageNum,pageSize, (int) pageInfo.getTotal(),pageInfo.getRecords());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new PageResult();
    }

    /**
     * 添加广告
     */
    @Override
    public void createAdvertise(HomeAdvertise advertise) {
        if (advertise == null){
            throw new BusinessException(ErrorResult.error());
        }
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        homeAdvertiseServiceMapper.insert(advertise);
    }

    /**
     * 删除广告
     */
    @Override
    public void deleteAdvertise(List<Long> ids) {
        homeAdvertiseServiceMapper.deleteBatchIds(ids);
    }

    /**
     * 修改上下线状态
     */
    @Override
    public void updateStatus(Long id,Integer status) {
        LambdaUpdateWrapper<HomeAdvertise> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(HomeAdvertise::getId,id);
        updateWrapper.set(HomeAdvertise::getStatus,status);
        homeAdvertiseServiceMapper.update(null,updateWrapper);
    }

    /**
     * 修改广告
     */
    @Override
    public void updateAdvertise(Long id, HomeAdvertise advertise) {
        advertise.setId(id);
        homeAdvertiseServiceMapper.updateById(advertise);
    }

    /**
     * 获取广告详情
     */
    @Override
    public HomeAdvertise advertise(Long id) {
        return homeAdvertiseServiceMapper.selectById(id);
    }

}
